<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->library('pagination');

		$config['base_url'] = '/manage/news?';
		
		
		$config['use_page_numbers'] = TRUE;
		//$config['enable_query_strings'] = TRUE;
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$config['first_link'] = '<<';
		$config['last_link'] = '>>';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		//$config['num_tag_open'] = '<li>';
		//$config['num_tag_close'] = '</li>';
		
		//	$config['anchor_class'] = "";
		
		//$rcount=0;

		$this->load->model('News_model');
		$pageindex = 1;
		if($this->input->get('p')!='') $pageindex=$this->input->get('p');

		$recordcount = 0 ;
		$result = $this->News_model->GetNewsPagination($pageindex,3,$recordcount);

		

		$config['total_rows'] = $recordcount;
		$config['per_page'] = 3; 
		$this->pagination->initialize($config); 
		$data['pagehtml'] = array('html' => $this->pagination->create_links()); 
		$data['news_list'] = $result;

		$this->load->library('session');
		$data['username'] = $this->session->userdata('username');
		$data['menuindex'] = 2;
		//print($rcount);
		$this->load->view('manage/header', $data);
		$this->load->view('manage/news_index',$data);
		$this->load->view('manage/foot');
	}

	/**
	*  新闻添加GET
	*/
	public function newsadd()
	{
		$this->load->model('Newscategory_model');
		$list = $this->Newscategory_model->GetCategoryTree();
		$data['category_list']=$list;
		$this->load->view('manage/header', $data);
		$this->load->view('manage/news_add',$data);	
		$this->load->view('manage/foot');
	}

	/**
	*  新闻添加POST
	*/
	public function newsaddaction()
	{
		$Title = $this->input->post('Title');
		$MetaTitle = $this->input->post('MetaTitle');
		$Image = $this->input->post('Image');
		$Summary = $this->input->post('Summary');
		$CategoryId = $this->input->post('CategoryId');
		$Url = $this->input->post('Url');
		$Content = $this->input->post('Content');
		$IsRecommend = $this->input->post('IsRecommend');
		if($IsRecommend==''){
			$IsRecommend = 0;
		}

		$this->load->model('News_model');
		$list = $this->News_model->Insert($Title,$MetaTitle,$Image,$Summary,$CategoryId,$Url,$Content,$IsRecommend);

		$this->load->helper('url');
		redirect('/manage/news');
	}

	/**
	*  新闻修改GET
	*/
	public function newsedit()
	{
		$this->load->model('Newscategory_model');
		$list = $this->Newscategory_model->GetCategoryTree();
		$data['category_list']=$list;
		$this->load->model('News_model');
		$entity = $this->News_model->GetEntity($this->input->get('id'));
		$data['model'] = $entity;
		$this->load->view('manage/header', $data);
		$this->load->view('manage/news_edit',$data);	
		$this->load->view('manage/foot');
	}
	

	/**
	*  新闻修改POST
	*/
	public function newseditaction()
	{
		$id = $this->input->post('id');
		$Title = $this->input->post('Title');
		$MetaTitle = $this->input->post('MetaTitle');
		$Image = $this->input->post('Image');
		$Summary = $this->input->post('Summary');
		$CategoryId = $this->input->post('CategoryId');
		$Url = $this->input->post('Url');
		$Content = $this->input->post('Content');
		$IsRecommend = $this->input->post('IsRecommend');
		if($IsRecommend==''){
			$IsRecommend = 0;
		}

		$this->load->model('News_model');
		$list = $this->News_model->Update($id,$Title,$MetaTitle,$Image,$Summary,$CategoryId,$Url,$Content,$IsRecommend);

		$this->load->helper('url');
		redirect('/manage/news');
	}

	/**
	* 删除新闻
	*/
	public function newsdel()
	{
		$id = $this->input->get('id');
		$this->load->model('News_model');
		$result = $this->News_model->Delete($id);
		//print_r($result);
		$this->load->helper('url');
		redirect('/manage/news');
	}



	/**
	*	分类管理
	*
	*/
	public function category()
	{
		$this->load->model('Newscategory_model');
		$list = $this->Newscategory_model->GetCategoryTree();
		$data['category_list']=$list;
		
		$this->load->view('manage/news_category',$data);
	}


	/**
	* 添加分类GET
	*
	*/
	public function categoryadd()
	{
		$this->load->model('Newscategory_model');
		$list = $this->Newscategory_model->GetCategoryTree();
		$data['category_list']=$list;
		$this->load->view('manage/news_categoryadd',$data);
	}

	/**
	* 添加分类POST
	*/
	public function categoryaddaction()
	{
		$name = $this->input->post('name');
		$description = $this->input->post('description');
		$parentid = $this->input->post('parentid');
		$this->load->model('Newscategory_model');
		$result = $this->Newscategory_model->AddCategory($name,$description,$parentid);
		$this->load->helper('url');
		redirect('/manage/news/category');
		//print_r($result);
		//header("Content-type:text/html;charset=utf-8");
		//echo $name.$description.$parentid;
	}

	/**
	*	修改分类GET
	*/
	public function categoryedit()
	{
		$this->load->model('Newscategory_model');
		$list = $this->Newscategory_model->GetCategoryTree();
		$data['category_list'] = $list;
		
		$model = $this->Newscategory_model->GetCategoryEntity($this->input->get('id'));
		$data['model'] = $model;
		//print_r($data['model']);
		$this->load->view('manage/news_categoryedit',$data);
	}

	/**
	*	修改分类POST
	*/
	public function categoryeditaction()
	{
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		$description = $this->input->post('description');
		$parentid = $this->input->post('parentid');
		$this->load->model('Newscategory_model');
		$result = $this->Newscategory_model->EditCategory($id,$name,$description,$parentid);
		
		$this->load->helper('url');
		redirect('/manage/news/category');
	}

	/**
	* 删除
	*/
	public function categorydel()
	{
		$id = $this->input->get('id');
		$this->load->model('Newscategory_model');
		$result = $this->Newscategory_model->DeleteCategory($id);
		//print_r($result);
		$this->load->helper('url');
		redirect('/manage/news/category');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */